package m202401.d06;

/**
 * @@author: 爱做梦的锤子
 * @date: 2024/1/6 09:27
 */
//2. 两数相加
public class Q2 {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode res = new ListNode(-1);
        ListNode p = res;
        int flag = 0;
        while (true) {
            int val = flag;
            if (l1 == null && l2 == null) {
                if (flag > 0) {
                    p.next = new ListNode(flag);
                }
                break;
            }
            if (l1 != null) {
                val = val + l1.val;
                l1 = l1.next;
            }
            if (l2 != null) {
                val = val + l2.val;
                l2 = l2.next;
            }
            flag = val / 10;
            p.next = new ListNode(val % 10);
            p = p.next;
        }
        return res.next;
    }

    static class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
            next = null;
        }
    }
}
